import xml.etree.ElementTree as xml
import brick,zipfile

class Level(object):
    def __init__(self,g):
        self.g=g
        self.color=[]
        self.hits=[]
        self.vis=[]
        z=zipfile.ZipFile('data.zip',"r")
        f='levels/level_'+str(self.g.nlvl)+'.xml'
        f=z.read(f)
        z.close()
        self.LoadLevel(f)
        k=0
        for y in range(self.g.height-64-8,self.g.height/4,-16):
            for x in range(32,self.g.width+32,64):
                b=brick.Brick(self.g,x,y,self.color[k],self.hits[k],self.vis[k])
                self.g.bricks.append(b)
                self.g.nbricks+=1
                k+=1

    def LoadLevel(self,f):
        doc=xml.fromstring(f)
        iter=doc.getiterator()
        for element in iter:
            if element.tag=='color':
                self.color.append(element.text)
            if element.tag=='hits':
                self.hits.append(element.text)
            if element.tag=='vis':
                self.vis.append(element.text)
